
<template>
  <div class="accountList"   >
    <div class="searchBox">
      <el-form :model="searchForm" ref="searchFormRef">
        <el-row>
          <el-form-item prop="name">
            <div class="item">
              <span v-html="'中文名称'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.name"
                placeholder="请输入中文名称"
              ></el-input>
            </div>
          </el-form-item>

          <el-form-item prop="inci">
            <div class="item">
              <span v-html="'INCI/英文名称'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.inci"
                placeholder="请输入中文名称"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="num">
            <div class="item">
              <span v-html="'目录序号'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.num"
                placeholder="请输入目录序号"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="searchClick"
              icon="el-icon-search"
              type="primary"
              size="mini"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div>
      <div class="topBtn" >
     
          <el-button
          size="mini"
            
            type="success"
            plain
            @click='downloadClick()'
            >下载模板</el-button
          >
     
      </div>
      <div class="topBtn" >
            <el-upload
          :headers="postHead"
          class="upload-demo"
          :action="$uploadRaw"
          :on-success="uploadSuccess"
          :before-upload="uploadbefore"
          :on-error="uploadError"
          :on-exceed="handleExceed"
          :show-file-list="false"
            :on-change='handleChange'
        >
          <el-button
          size="mini"
            v-has="'ingredient:self:import'"
            type="success"
            plain
            
            >导入原料</el-button
          >
        </el-upload>
          
      </div>
      
      <div class="topBtn">
        <el-button
          v-has="'ingredient:self:add'"
          size="mini"
          @click="addRawClick"
          type="success"
          plain
          icon="el-icon-plus"
          >新增原料</el-button
        >
      </div>
    </div>
<div>
  <el-progress
                v-if="showJinDuTao"
                style="width: 150px; margin-top: 10px;float: right;
    margin-right: 63px;"
                :percentage="progressPercentExcel"
              />
</div>
    <div class="tableBox" ref="tableBoxRef">
      <el-table
       :height="605"
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="num" align="center" label="目录序号">
        </el-table-column>
        <el-table-column prop="name" align="center" label="中文名称">
        </el-table-column>
        <el-table-column prop="inci" align="center" label="INCI/英文名称">
        </el-table-column>
        <el-table-column prop="casWrap" align="center" label="CAS号">
          <template slot-scope="scope">
            {{ scope.row.casWrap.join(",") }}
          </template>
        </el-table-column>
        <el-table-column prop="ecWrap" align="center" label="EC号">
          <template slot-scope="scope">
            {{ scope.row.ecWrap.join(",") }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'ingredient:self:info'"
              @click="rawDetailClick(scope.row)"
              >详情</el-button
            > -->
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'ingredient:self:update'"
              @click="editClick(scope.row)"
              >编辑</el-button
            >
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="text"
              @click="delClick(scope.row)"
              v-has="'ingredient:self:delete'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-card class="footer" v-if="total > 0">
         <div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[15, 30, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
       </el-card>
    </div>
     <el-dialog :close-on-click-modal="false" 
      title="导入原料"
      :visible.sync="changeVisible"
      width="700px"
      class="addEditAccountDialog"
      v-loading="loading"
    >
        <div style="color: #2d81ff;float:right">下载模板</div>
        <div style="text-align:center;margin:60px 0">
              <el-upload
          :headers="postHead"
          class="upload-demo"
          :action="$uploadRaw"
          :on-success="uploadSuccess"
          :before-upload="uploadbefore"
          :on-error="uploadError"
          :on-exceed="handleExceed"
          :show-file-list="false"
        >
          <el-button
            v-has="'ingredient:self:import'"
            type="success"
            plain
            
            >上传文件</el-button
          >
        </el-upload>
        </div>
        <div class="btns">
        <el-button
          v-clickForbidden
          size="mini"
          type="primary"
          @click="changeVisible = false"
          >确 定</el-button
        >
      </div>
       </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
        progressPercentExcel: 0, // 进度条默认为0
      showJinDuTao: false, //控制进度条显示隐藏
      changeVisible:false,
            postHead: { token: localStorage.token },
      searchForm: {
        name: "",
        num: "",
        inci: "",
      },
      loading:false,
      tableLoading: false,
      tableHeight: 50,
      tableData: [],
      pageNum: 1,
      pageSize: 15,
      total: 0,
    };
  },
  computed: {
    ...mapGetters(["dictList"]),
  },

  created() {
    this.getRawList();
  },
  methods: {
        handleChange( file, fileList){
          console.log(file);
    
         if(file.status==='ready'){
          this.progressPercentExcel =0
          this.showJinDuTao=true
          const interval=setInterval(()=>{
            if(this.progressPercentExcel>=99){
              clearInterval(interval)
              return
            }
            this.progressPercentExcel+=1
          },100)
      }
      if(file.status==='success'){
        this.progressPercentExcel=100
         setTimeout(()=>{
          this.progressPercentExcel = 0;
          this.showJinDuTao=false
        },1000)
      }
       if(file.status==='fail'){
         setTimeout(()=>{
          this.progressPercentExcel = 0;
          this.showJinDuTao=false
        },1000)
      }
  
     
    },
       async downloadClick() {
        const fileUrl = 'https://pl-attachment01.oss-cn-beijing.aliyuncs.com/%E5%8E%9F%E6%96%99%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx' // 文件的URL地址
      const link = document.createElement('a');
      link.href = fileUrl;
      link.setAttribute('download', '原料导入模板.xlsx');
      link.click();
      this.$message.success("请稍等,正在下载");
    },
    uploadYl(){
        this.changeVisible=true
    },
    rawDetailClick(row) {
      this.$router.push({
        path: "/rawManagement/rawDetai",
        query: { id: row.id, type: "view" },
      });
    },
    editClick(row) {
      this.$router.push({
        path: "/rawManagement/rawDetai",
        query: { id: row.id, type: "edit" },
      });
    },
       handleExceed(files, fileList) {
      this.$message.warning("只能上传一个附件");
    },
    // 上传文件前的钩子
    uploadbefore() {
      this.$message.warning("开始导入,请稍等");
      this.loading=true
     
    },
    // 上传文件失败的钩子
    uploadError(err) {
      let myError=err.toString();
       myError=myError.replace("Error: ","")//去掉前面的
      myError=JSON.parse(myError);//转对象
      this.$message.error(myError.message);
        this.loading=false
    },
    // 上传文件成功的钩子
    uploadSuccess(resp, file, fileList) {
      if (resp.code!='00000') {
         this.loading=false
        this.$message.error(resp.message);
      } else {
          this.loading=false
        this.$message.success("导入Excel成功");
        this.getRawList();
      }

    },
    resetClick() {
      this.$refs.searchFormRef.resetFields();
      this.pageNum = 1;
      this.getRawList();
    },
    searchClick() {
      this.pageNum = 1;
      this.getRawList();
    },
    /**
     * 获取标准库列表
     * @param {*}
     */
    async getRawList() {
      this.tableLoading = true;
      const res = await this.$api.rawmanList({
        size: this.pageSize,
        page: this.pageNum,
        condition: {
          ...this.searchForm,
        },
      });
      this.total = res.data.result.total;
      this.tableData = res.data.result.records;

      this.tableLoading = false;
    },

    /**
     * 新增文库
     * @param {*}
     */
    addRawClick() {
      this.$router.push({
        path: "/rawManagement/rawDetai",
        query: { type: "add" },
      });
    },
    /**
     * 删除按钮
     * @param {*} row
     */
    delClick(row) {
      this.$confirm("此操作不可恢复，确认继续删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api.delRawman(row.id).then((res) => {
            if (res.data.code == "00000") {
              this.$message.success("删除成功");
              this.getRawList();
            }
          });
        })
        .catch(() => {});
    },

    /**
     * 切换页面大小
     * @param {*} val
     */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getRawList();
    },

    /**
     * 页码切换
     * @param {*} val
     */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getRawList();
    },
  },
};
</script>

<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
  .topBtn {
    margin: 10px 0;
    display: inline-block;
    margin-right: 20px;
    float: right;
  }
  .addEditAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
</style>